package com.th.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.th.bean.Category;
import com.th.common.R;
import com.th.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    //初始显示数据
    @GetMapping("/page")
    public R<Page> getall(int page,int pageSize){
        System.out.println("数据"+page+pageSize);
        Page<Category> cgp = new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getSort);

        categoryService.page(cgp,lqw);

        return R.success(cgp);
    }

    @PostMapping
    public R<String> add(@RequestBody Category category){
        log.info("添加操作");
        categoryService.save(category);

        return R.success("添加成功");
    }

    @DeleteMapping()
    public R<String> delete(Long ids){
        log.info("删除操作");

        categoryService.remove(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> updata(@RequestBody Category category){
        log.info("更新操作");
        categoryService.updateById(category);

        return R.success(null);
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> clw = new LambdaQueryWrapper<>();
        clw.eq(category.getType()!=null,Category::getType,category.getType());
        clw.orderByDesc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(clw);
        return R.success(list);

    }


}
